package cn.pengyinrui.pethome.dept.controller;

import cn.pengyinrui.pethome.base.controller.BaseController;
import cn.pengyinrui.pethome.dept.domain.Department;
import cn.pengyinrui.pethome.dept.query.DepartmentQuery;
import cn.pengyinrui.pethome.dept.service.IDepartmentService;
import cn.pengyinrui.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.reflect.generics.tree.Tree;

import java.util.List;

@RestController
@RequestMapping("/dept")
// 对controller做描述
@Api(value = "部门的API",description = "部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    //@RequestMapping(value = "list",method = RequestMethod.GET)
    @GetMapping("/list")
    // 对方法做描述
    @ApiOperation(value = "查询全部的部门")
    public AjaxResult findAll(){
        try {
            return AjaxResult.success()
                    .setData(service.findAll())
                    .setTotal(service.findAllByQueryCount(new DepartmentQuery()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PostMapping("/{id}")
    @ApiOperation(value = "根据id查询部门")
    public AjaxResult findOne(@PathVariable("id") Long id){
        try {
            return AjaxResult.success()
                    .setData(service.findOne(id))
                    .setTotal(service.findAllByQueryCount(new DepartmentQuery()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PostMapping("/query")
    @ApiOperation(value = "分页查询")
    public AjaxResult findAllByQuery(@RequestBody DepartmentQuery query){
        try {
            return AjaxResult.success()
                    .setData(service.findAllByQuery(query))
                    .setTotal(service.findAllByQueryCount(query));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除部门")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error().setTip(e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation(value = "保存,如果数据有id就修改，没有就新增")
    public AjaxResult save(@RequestBody Department department){
        try {
            if (department.getId() != null) {
                service.update(department);
            } else {
                service.add(department);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setTip(e.getMessage());
        }
        return AjaxResult.success();
    }

    @PatchMapping
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            service.batchDel(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setTip(e.getMessage());
        }
        return AjaxResult.success();
    }

    @GetMapping("/tree")
    @ApiOperation(value = "部门无限极树")
    public List<Department> getTree(){
        return service.deptTree();
    }
}
